package com.lu.service;

import com.lu.mapper.FriendsMapper;
import com.lu.mapper.MessagesMapper;
import com.lu.pojo.Friends;
import com.lu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class FriendsService {

    @Resource
    private MessagesMapper messagesMapper;
    @Resource
    private UserService userService;
    @Resource
    private FriendsMapper friendsMapper;
    public List<Friends> getFriend(int friendid, int fuserid){
        Map<String, Object> map = new HashMap<>();
        map.put("friendid",friendid);
        map.put("fuserid",fuserid);
        return friendsMapper.selectByMap(map);
    }
    public int addFriend(int friendid, int fuserid,String fname){
        Friends friends1 = new Friends();
        friends1.setFriendid(fuserid);
        friends1.setFuserid(friendid);
        friends1.setFname(null);
        friendsMapper.insert(friends1);
        Friends friends = new Friends();
        friends.setFriendid(friendid);
        friends.setFuserid(fuserid);
        friends.setFname(null);
        return friendsMapper.insert(friends);
    }
    public List<User> getFriends(int fuserid){
        Map<String, Object> map = new HashMap<>();
        map.put("fuserid",fuserid);
        List<Friends> friends = friendsMapper.selectByMap(map);
        List<User> user2 =new ArrayList<>();
        for (Friends friend : friends) {
            assert false;
            user2.addAll(userService.getUser2(friend.getFriendid()));
        }
        return user2;
    }
    public int DeleteFriend(int fuserid ,int friendid){
        Map<String, Object> map = new HashMap<>();
        map.put("fuserid",fuserid);
        map.put("friendid",friendid);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("fuserid",friendid);
        map3.put("friendid",fuserid);
        friendsMapper.deleteByMap(map3);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("mfromuserid",fuserid);
        map1.put("mtouserid",friendid);
        messagesMapper.deleteByMap(map1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("mfromuserid",friendid);
        map2.put("mtouserid",fuserid);
        messagesMapper.deleteByMap(map2);
        return friendsMapper.deleteByMap(map);
    }

}
